<?php
namespace app\admin\controller;

use app\common\model\Extract as ExtractModel;
use app\common\controller\AdminBase;
use think\Config;
use think\Db;
use think\Request;
/**
 * 会员提现
 * Class AdminUser
 * @package app\admin\controller
 */
class Extract extends AdminBase
{
    protected $extract_model;

    protected function _initialize()
    {
        parent::_initialize();
        $this->extract_model = new ExtractModel();
    }

    /**
     * 会员提现记录列表
     * @param int    $page
     * @return mixed
     */
    public function index($keyword = '',$wallet_type_id = '',$status_type_id = '',$page = 1,$begin_time='',$end_time='')
    {
        $map_join = [];
        // 关键字查询条件
        if ($keyword) {                                                     
            $map_join['u.user_name|u.mobile|u.email'] = ['like', "%{$keyword}%"];
        }
        // 钱包类型搜索
        if (isNum($wallet_type_id)) {                                       
            $map_join['e.wallet_type'] = ['=', "$wallet_type_id"];          
        }
        // 处理类型搜索
        if (isNum($status_type_id)) {                                       
            $map_join['e.status'] = ['=', "$status_type_id"];          
        }
        // 获取钱包类型---搜索用
        $wallet_type = wallet_type();  
        // 获取状态类型---搜索用                                     
        $status_type = status_type();                                       
        // 时间检索
        if($begin_time && $end_time){                                                    
            $begin = strtotime($begin_time);
            $end   = strtotime($end_time);
            $map_join['e.time'] = array('between',["$begin","$end"]);
        }
        // 获取提现列表
        $list   =    db('extract')                                          
                    ->alias('e')
                    ->where($map_join)
                    ->join('user u','u.id = e.uid')
                    ->field('e.*,u.user_name as user_name')
                    ->order('e.id DESC')->paginate(15, false, ['page' => $page,'query' => Request::instance()->param()]);
        // 统计总量
        $extract['sum_num'] = db('extract')                                  
                    ->alias('e')
                    ->where($map_join)
                    ->join('user u','u.id = e.uid')
                    ->sum('num');

        return $this->fetch('index', ['list' => $list,'keyword' => $keyword, 'wallet_type' => $wallet_type, 'status_type' => $status_type, 'wallet_type_id' => $wallet_type_id, 'status_type_id' => $status_type_id,'extract' => $extract,'begin_time'=>$begin_time==''? '':$begin_time,'end_time'=>$end_time==''? '':$end_time]);
    }


    /**
     * 会员提现审核通过
     * @param int  id
     * @return 1
     */
    public function edit() {


            $id = $_POST['id'];

            $actives = db('extract')->where(array('id'=>$id))->find();

            if($actives){

                db('extract')->where(array('id'=>$id))->setField(array('status'=>1));
            }

            $this->success ( '审核成功' );

    }


    /**
     * 会员提现审核拒绝
     * @param int  id
     * @return 1
     */
    public function refuse() {


        $id = $_POST['id'];

        $actives = db('extract')->where(array('id'=>$id))->find();

        if($actives){
            // 补充拒绝提现退款
            // 钱包类型为增值宝
            if ($actives['wallet_type'] == '7') {
                $type_name = 'balance';
            }elseif ($actives['wallet_type'] == '8') {
                $type_name = 'shop_money';
            }
            // 查询会员信息
            $user_info = db('user')->where(" id = {$actives['uid']}")->find();
            // 更新会员钱包
            $up_user[$type_name] = $user_info[$type_name] + $actives['num'];
            model('user')->allowField(true)->save($up_user,['id' => $actives['uid']]);
            // 记录
            $log[] =  array(
                            'num'           => $actives['num'],
                            'wallet_type'   => $actives['wallet_type'],
                            'change_type'   => '7',// 拒绝提现
                            'time'          => time(),
                            'uid'           => $actives['uid'],
                            'before_num'    => $user_info[$type_name],
                            'later_num'     => $up_user[$type_name],
                            'description'   => '拒绝提现退款'.$actives['num'],
                        );
            model('fund_log')->saveAll($log);
            db('extract')->where(array('id'=>$id))->setField(array('status'=>2));

        }

        $this->success ( '已拒绝' );

    }

    /**
     * 提现导出
     * @return mixed
     */
    public function daochu($status='') {
        $title = "用户提现";
        $titlename = "<tr>
           <th>用户名</th>
           <th>提现金额</th>
           <th>状态</th>
           <th>提现银行</th>
           <th>提现账号</th>
           <th>真实姓名</th>
           <th>手机号码</th>
           <th>提现时间</th>
        </tr>";
        if (isNum($status)) {                                       
            $map_join['e.status'] = ['=', "$status"];          // 钱包类型搜索
        }
        $res = db('extract')
                ->alias('e')
                ->where($map_join)
                ->join('user u','u.id = e.uid')
                ->field('e.*,u.user_name as user_name')
                ->order('e.id DESC')
                ->select();
        //p($res);die;
        $status_type = status_type();
        foreach ($res as $key => $value) {
            $arr[$key]['user_name']= $value['user_name'];
            $arr[$key]['num'] = $value['num'];
            $arr[$key]['status'] = $status_type[$value['status']];
            $arr[$key]['bank_name']= $value['bank_name'];
            $arr[$key]['card_id']= $value['card_id'];
            $arr[$key]['true_name']= $value['true_name'];
            $arr[$key]['mobile']= $value['mobile'];
            $arr[$key]['time']= date_time($value['time']);
        }
        $dataResult = $arr;      //todo:导出数据（自行设置）
        $filename = $title.".xls";
        excelData($dataResult,$titlename,$title,$filename);
    }
}